<template>
  <div class="main">
    <div style="display: flex; justify-content: center">
      <div class="content">
        <h2 style="text-align: center; margin-bottom: 95px">
          {{ t("标题") }}
        </h2>
        <div class="from">
          <el-tabs v-model="type">
            <el-tab-pane :label="t('使用手机号注册')" name="1">
              <div class="tabCont">
                <input
                  type="text"
                  v-model="phone"
                  :placeholder="t('请输入手机号')"
                />
                <div class="verify">
                  <input
                    type="text"
                    v-model="phone_code"
                    :placeholder="t('请输入验证码')"
                  />
                  <el-button
                    class="countdown"
                    type="text"
                    @click="countdownClick()"
                    :disabled="phone_is_count"
                  >
                    {{ phone_countdown }}
                  </el-button>
                </div>
                <input
                  type="password"
                  v-model="phone_password"
                  :placeholder="t('设置新密码')"
                />
                <div class="hint">
                  <router-link to="register" tag="p" class="register">{{
                    t("注册")
                  }}</router-link>
                </div>
              </div>
              <button class="register-btn" @click="registerClick()">
                {{ t("注册") }}
              </button>
            </el-tab-pane>
            <el-tab-pane :label="t('使用邮箱注册')" name="2">
              <div class="tabCont">
                <input
                  type="text"
                  v-model="email"
                  :placeholder="t('请输入邮箱账号')"
                />
                <div class="verify">
                  <input
                    type="text"
                    v-model="email_code"
                    :placeholder="t('请输入验证码')"
                  />
                  <el-button
                    class="countdown"
                    type="text"
                    @click="countdownClick()"
                    :disabled="email_is_count"
                  >
                    {{ email_countdown }}
                  </el-button>
                </div>
                <input
                  type="text"
                  v-model="email_password"
                  :placeholder="t('设置新密码')"
                />
                <div class="hint">
                  <router-link to="register" tag="p" class="register">{{
                    t("注册")
                  }}</router-link>
                </div>
              </div>
              <button class="register-btn" @click="registerClick()">
                {{ t("注册") }}
              </button>
            </el-tab-pane>
          </el-tabs>
        </div>
        <div class="rests">
          <p>{{ t("其他方式登录") }}</p>
          <img
            src="../../assets/images/home/facelogin.png"
            alt=""
            class="icon"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { resetPwd, smsSend, emsSend } from "@/utils/api.js";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      /**
       * 1:手机 2:邮箱
       */
      type: "1",
      phone: "",
      phone_password: "",
      phone_code: "",
      phone_countdown: this.t("获取验证码"),
      phone_is_count: false,
      email: "",
      email_password: "",
      email_code: "",
      email_countdown: this.t("获取验证码"),
      email_is_count: false,
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    /**
     * 简化翻译方法(少写property)
     * @param {String} v 最后一个key值
     */
    t(v) {
      return this.$t(`登录.${v}`);
    },
    countdownClick() {
      let phone_min = 60;
      let email_min = 60;
      let promise = null;
      if (this.type == "1") {
        if (!this.$util.checkPhone(this.phone)) {
          this.$message({
            message: this.t("请输入正确的手机号"),
            type: "error",
          });
          return;
        }
        this.phone_is_count = true;
        this.phone_countdown = phone_min;
        let phone_timer = setInterval(() => {
          this.phone_countdown--;
          if (this.phone_countdown == 0) {
            clearInterval(phone_timer);
            phone_timer = null;
            this.phone_countdown = this.t("获取验证码");
            this.phone_is_count = false;
          }
        }, 1000);
        promise = smsSend({ mobile: this.phone });
      } else if (this.type == "2") {
        this.email_countdown = email_min;
        this.email_is_count = true;
        let email_timer = setInterval(() => {
          this.email_countdown--;
          if (this.email_countdown == 0) {
            clearInterval(email_timer);
            email_timer = null;
            this.email_countdown = this.t("获取验证码");
            this.email_is_count = false;
          }
        }, 1000);
        smsSend({ email: this.email });
      }
      promise.then((res) => {
        if (res.code == 1) {
          this.$message({
            message: res.msg,
            type: "success",
          });
        }
      });
    },
    registerClick() {
      let data = {
        type: this.type,
      };
      if (this.type == "1") {
        data = {
          ...data,
          mobile: this.phone,
          new_password: this.phone_password,
          code: this.phone_code,
        };
      } else if (this.type == "2") {
        data = {
          ...data,
          email: this.email,
          new_password: this.email_password,
          code: this.email_code,
        };
      }
      resetPwd(data).then((res) => {
        if (res.code == 1) {
          this.$message({
            message: res.msg,
            type: "success",
          });
          this.$router.replace("login");
        }
      });
    },
  },
  beforeCreate() {}, //生命周期 - 创建之前
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  beforeMount() {}, //生命周期 - 挂载之前
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
};
</script>
<style lang="less" scoped>
.main {
  .content {
    margin-top: 120px;
    width: 420px;
    height: 750px;
    position: relative;

    h2 {
      font-size: 39px;
      font-family: Source Han Sans SC;
      font-weight: bold;
      color: #000000;
      line-height: 99px;
    }

    .from {
      display: flex;
      justify-content: center;

      .tabCont {
        width: 100%;
        display: flex;
        justify-content: center;
        flex-flow: row wrap;
        margin-top: 20px;
        margin-bottom: 40px;

        input {
          width: 100%;
          height: 60px;
          background: #f9f9f9;
          border-radius: 10px;
          border: 0px;
          outline: none;
          text-indent: 1em;
          margin-bottom: 20px;
        }

        .verify {
          position: relative;
          width: 100%;
          height: 60px;
          display: flex;
          justify-content: flex-start;
          align-items: center;
          flex-direction: row;

          input {
            width: 100%;
            height: 60px;
          }

          .countdown {
            position: absolute;
            top: 0;
            right: 10px;
            font-size: 16px;
            font-weight: 400;
            color: #ff5757;
          }
        }

        .hint {
          width: 100%;
          height: 40px;
          font-size: 14px;
          font-weight: bold;

          .register {
            display: inline;
            float: right;
            font-size: 14px;
            font-weight: 500;
            color: #ff5757;
            line-height: 38px;
            cursor: pointer;
          }
        }
      }

      .tabContCode {
        width: 373px;
        height: 373px;
        background: #f9f9f9;
        border-radius: 20px;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;

        img {
          width: 260px;
          height: 260px;
        }

        p {
          width: 126px;
          height: 14px;
          font-size: 14px;
          font-weight: 400;
          color: #333333;
          margin-top: 15px;
        }
      }

      .register-btn {
        width: 100%;
        height: 60px;
        background: #ff5757;
        border-radius: 10px;
        color: #fff;
        text-align: center;
        line-height: 40px;
        border: 0px;
        font-size: 18px;
      }
    }

    .rests {
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      position: absolute;
      bottom: 0px;

      p {
        font-size: 16px;
        font-weight: 400;
        color: #999999;
        margin-bottom: 10px;
      }

      img {
        width: 39px;
        height: 39px;
      }
    }

    /deep/ .el-tabs__item.is-active {
      color: #000;
      font-weight: bold;
    }

    /deep/ .el-tabs__item {
      width: 210px;
      text-align: center;
      color: #666;
      font-size: 16px;
    }

    /deep/ .el-tabs__active-bar {
      background-color: #ff5757;
    }

    /deep/ .el-tabs__item:hover {
      color: #000;
    }
  }
}
</style>
